import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_LINE_LOOP;
import static org.lwjgl.opengl.GL11.GL_LINE_STRIP;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW_MATRIX;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glFlush;
import static org.lwjgl.opengl.GL11.glFrustum;
import static org.lwjgl.opengl.GL11.glGetDouble;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTranslated;
import static org.lwjgl.opengl.GL11.glVertex3d;
import static org.lwjgl.opengl.GL11.glVertex3f;
import static org.lwjgl.opengl.GL11.glViewport;

import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.util.ArrayList;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class GLWindow extends Thread implements GraphicsEngineI{
	
	public float FPS;
	private int numFrames;
	private float fpsUpdateInterval;
	private float lastUpdate;
	
	private static final double DEFAULTLENGTH = 5;
	private double[] location;
	private double[] directionVector;
	
	public GLWindow() {
		FPS = 0;
		numFrames = 0;
		fpsUpdateInterval = (float) 0.5;
		lastUpdate = System.nanoTime();
	}
	
	public void updateFPS() {
		numFrames += 1;
		float currentUpdate = System.nanoTime();
		
		if( currentUpdate - lastUpdate > fpsUpdateInterval )
		  {
		    FPS = numFrames / (currentUpdate - lastUpdate);
		    lastUpdate = currentUpdate;
		    numFrames = 0;
		  }
	}
	
	//in the run method of GLWindow, an FPS counter will have to be going on
	//This will be used to create the illusion of fluid movement for the Cart
	public void run() {
		try
		{

			Display.setDisplayModeAndFullscreen(new DisplayMode(1280, 1024));
			Display.create();

		} catch (Exception e)
		{
		}

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 200.0);

		glMatrixMode(GL_MODELVIEW);
		glViewport(0, 0, 1280, 1024);
		
		while (!Display.isCloseRequested())
		{
			updateFPS();
			try
			{
				display();
			} catch (Exception e)
			{
				e.printStackTrace();
				System.exit(1);
			}

			// You see this line?
			Display.update();
			// Don't you DARE remove it!
			// Doing so will lock up the entire machine!

		}
		Display.destroy();
	}
	
	public void display() {
		glMatrixMode(GL_MODELVIEW);
		glClear(GL_COLOR_BUFFER_BIT);
		glLoadIdentity();
		
		
	}

	@Override
	public void openWindow() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderCart(Cart cart) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderCoaster(Rollercoaster coaster) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderPart(Part part) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderRightCurve() {
		// TODO Auto-generated method stub
		
	}
	
	public void renderDefaultStrightPiece() {
		renderStraightPiece(DEFAULTLENGTH);
	}
	
	public void renderStraightPiece(double length) {
		ArrayList<double[]> piece = new ArrayList<double[]>();
		int steps = (int) length * 100;

		for (int i = 0; i < steps; i++)
		{
			double[] point = new double[] { i * .01 * directionVector[0] + location[0], i * .01 * directionVector[1] + location[1], i * .01 * directionVector[2] + location[2] };
			piece.add(point);
		}
		//add to path?
		return piece;
	}

	@Override
	public void renderUpCurve() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderDownCurve() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderLeftCurve() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void renderScreenImage(float x, float y, float width, float height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderScreenRectangle(float x, float y, float width,
			float height) {
		// TODO Auto-generated method stub
		
	}

	

	@Override
	public void renderWorldCube(float x, float y, float z, float width,
			float height, float depth) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderWorldRectangle(float x, float y, float width, float height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void runCoaster() {
		// TODO Auto-generated method stub
		
	}
}
